package com.xinzhi.service.impl;

import com.xinzhi.log.service.ITransferLog;
import com.xinzhi.log.service.IncomeAndExpensesLog;
import com.xinzhi.mapper.TransferAccountsMapper;
import com.xinzhi.pojo.Bankaccount;
import com.xinzhi.pojo.Incomeandexpenditurerecord;
import com.xinzhi.pojo.dto.TransferAccounts;
import com.xinzhi.service.ITransferAccountsService;
import com.xinzhi.service.PayeeService;
import com.xinzhi.service.recordService.IIncomeAndExpensesService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Transactional

public class TransferAccountsServiceImpl implements ITransferAccountsService{
    @Autowired
    private TransferAccountsMapper transferAccountsMapper;
    @Autowired
    private PayeeService payeeService;


    @Autowired
    private ITransferLog transferLog;
    @Autowired
    private IncomeAndExpensesLog incomeAndExpensesLog;
    private static final long TRANSFER = 0;

//    private static final long TRANSFER_DELAY_MS = 2*60*60000; //2小时
    private static final long TRANSFER_DELAY_MS = 30000; //30秒
    private static final long DAY_IN_MS = 24 * 60 * 60000; // 24小时的毫秒数


    /**
     * 转账
     * @param transferAccounts 转账参数
     * @return
     */
    @Override
    public Result transfer(TransferAccounts transferAccounts) {

        Bankaccount fromBank = transferAccountsMapper.selectMoney(transferAccounts.getFrom());
        if (transferAccounts.getMoney() == null)
            return Result.error("汇款金额不能为空");
        if (transferAccounts.getMoney() ==0)
            return Result.error("输入的金额不能为零！");
        if (transferAccounts.getMoney() <0)
            return Result.error("请输入正确的金额");
        if (transferAccounts.getFrom().equals(transferAccounts.getTo()))
            return Result.error("请不要给自己转账");
        if (fromBank.getMoney() < transferAccounts.getMoney())
            return Result.error("余额不足");
        if (!transferAccounts.getPassword().equals(fromBank.getCardPassword()))
            return Result.error("密码错误");
        Bankaccount toBank = transferAccountsMapper.selectMoney(transferAccounts.getTo());
        if (toBank == null)
            return Result.error("收款人账户不存在");
        if (fromBank.getMoney() < transferAccounts.getMoney())
            return Result.error("转账失败");
        Timer timer = new Timer();
        //定时任务
        long transfer1;
        Date midnight = null;
        switch (transferAccounts.getType()){
            case "实时汇款":
                transfer1 = TRANSFER;
                break;
            case "普通汇款":
                transfer1 = TRANSFER_DELAY_MS;
                break;
            case "次日汇款":
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                midnight = calendar.getTime();
                // 如果当前时间已经过了今日的凌晨，则设定为明日的凌晨
                if (midnight.before(new Date()))
                    midnight = new Date(midnight.getTime() + DAY_IN_MS);
                transfer1 = midnight.getTime() - System.currentTimeMillis();
                break;
            default:
                    return Result.error("请输入正确的类型");
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                transferAccountsMapper.updateMoneyByCardNumber(transferAccounts.getFrom(), fromBank.getMoney() - transferAccounts.getMoney());
                transferAccountsMapper.updateMoneyByCardNumber(transferAccounts.getTo(),toBank.getMoney() + transferAccounts.getMoney());
                //异步转账记录
                transferLog.addTransfer(fromBank, toBank,transferAccounts.getMoney());
                double v = 0 - transferAccounts.getMoney();
                //异步收入和支出记录
                incomeAndExpensesLog.addIncomeAndExpenses(fromBank, toBank,v);
                incomeAndExpensesLog.addIncomeAndExpenses(toBank, fromBank,transferAccounts.getMoney());
                //新增收款账户
                payeeService.insertPayee(transferAccounts.getTo(),fromBank.getId());


            }
            }, transfer1);


        return Result.ok("转账成功"+transferAccounts.getMoney());
    }



    @Override
    public Incomeandexpenditurerecord addIcomeAndExpenditureRecord(Bankaccount from, Bankaccount to) {
        //自己
        Incomeandexpenditurerecord incomeandexpenditurerecord = new Incomeandexpenditurerecord();
        incomeandexpenditurerecord.setUserId(from.getId());
        incomeandexpenditurerecord.setTransactionCard(from.getCardNumber());
        incomeandexpenditurerecord.setTransactionAccount(from.getUsername());
        incomeandexpenditurerecord.setTransactionType("网转");
        incomeandexpenditurerecord.setTradingFloor("手机银行");
        //对方
        incomeandexpenditurerecord.setReciprocalCard(to.getCardNumber());
        incomeandexpenditurerecord.setReciprocalName(to.getUsername());
        incomeandexpenditurerecord.setReciprocalOpeningBank(to.getOpeningBank());
        return incomeandexpenditurerecord;
    }
}
